package com.example.project.controller;

import com.example.project.dto.CheckAreaPageDTO;
import com.example.project.entity.CheckArea;
import com.example.project.service.CheckAreaService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;

/**
 * (CheckArea)表控制层
 *
 * @author makejava
 * @since 2024-02-23 15:18:17
 */
@RestController
@RequestMapping("checkArea")
public class CheckAreaController {
    /**
     * 服务对象
     */
    @Resource
    private CheckAreaService checkAreaService;

    /**
     * 分页查询
     *
     *  checkArea 筛选条件
     *  pageRequest      分页对象
     * @return 查询结果
     */
    @CrossOrigin
    @PostMapping("findPage")
    public ResponseEntity<Page<CheckArea>> queryByPage(@RequestBody CheckAreaPageDTO data) {
        com.example.project.entity.Page page = data.getPage();
        return ResponseEntity.ok(this.checkAreaService.queryByPage(data.getCheckArea(),PageRequest.of(page.getPage(),page.getSize())));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @CrossOrigin
    @GetMapping("{id}")
    public ResponseEntity<CheckArea> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.checkAreaService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param checkArea 实体
     * @return 新增结果
     */
    @CrossOrigin
    @PostMapping("add")
    public ResponseEntity<CheckArea> add(@RequestBody CheckArea checkArea) {
        return ResponseEntity.ok(this.checkAreaService.insert(checkArea));
    }

    /**
     * 编辑数据
     *
     * @param checkArea 实体
     * @return 编辑结果
     */
    @CrossOrigin
    @PutMapping("put")
    public ResponseEntity<CheckArea> edit(@RequestBody CheckArea checkArea) {
        return ResponseEntity.ok(this.checkAreaService.update(checkArea));
    }

    /**
     * 删除数据
     *
     * param data.getCheckAreaId() 主键
     * @return 删除是否成功
     */
    @CrossOrigin
    @DeleteMapping("del")
    public ResponseEntity<Boolean> deleteById(@RequestBody CheckArea data) {
        return ResponseEntity.ok(this.checkAreaService.deleteById(data.getCheckAreaId()));
    }

}

